package com.ctsi.organise.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.organise.common.constant.PartyType;
import com.ctsi.organise.entity.OrganiseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;


/**
 * OrganiseEntity Service 类
 * @author   zhangjw
 * @description  OrganiseEntity
 * @created	 Sun Nov 11 16:35:04 CST 2018
 */

@Repository
public class OrganiseEntityManager {
	private static final Logger logger=LoggerFactory.getLogger(OrganiseEntityManager.class);
	@Autowired
	private MybatisGenericDao dao;

	/**
	 * 添加
	 */
	public void insertOrganiseEntity(OrganiseEntity organiseEntity){
		if(UtilValidate.isEmpty(organiseEntity.getId())) {
            organiseEntity.setId(UUIDGenerator.getId());
        }
		logger.debug("insert {}",organiseEntity);
		dao.insert(organiseEntity);
	}	
	/**
	 * 动态更新
	 */	
	public int updateOrganiseEntityByPKSelective(OrganiseEntity organiseEntity){
		logger.debug("updateByPrimaryKeySelective {}",organiseEntity);
		return dao.update(OrganiseEntity.class.getName()+".updateByPrimaryKeySelective", organiseEntity);
	}
	/**
	 * 更新
	 */
	public int updateOrganiseEntity(OrganiseEntity organiseEntity){
		logger.debug("update {}",organiseEntity);
		return dao.update(organiseEntity);
	}
	
	
	public OrganiseEntity editOrganiseEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("editById {}",id);
		return (OrganiseEntity)dao.queryForObject(OrganiseEntity.class.getName()+".edit", id);
	}
	/**
	 * 根据ID查找 OrganiseEntity对象
	 */
	public OrganiseEntity findOrganiseEntityById(String id){
		if(UtilValidate.isEmpty(id))
			return null;
		logger.debug("findById {}",id);
		return dao.get(OrganiseEntity.class, id);
	}
	/**
	 * 删除OrganiseEntity对象
	 */
	public int removeOrganiseEntity(String id){	
		if(UtilValidate.isNotEmpty(id))
			return dao.delete(OrganiseEntity.class.getName()+".delete", id);
			//return dao.remove(id);
		return 0;
	}
	
	/**
	 * 详细页查询
	 */
	public OrganiseEntity findByDetail(String id){
		return (OrganiseEntity)dao.queryForObject(OrganiseEntity.class.getName()+".findByDetail",id);
	}
	/** 
	 * 多条件查询结果 
	 */
	public Page<OrganiseEntity> findByMulitCondition(Map<String,Object> param , int start , int pageSize){
		logger.debug("findByMulitCondition {}",param);
		if(pageSize<0||pageSize>2000)
			pageSize=Page.DEFAULT_PAGE_SIZE;
		if(start!=-1)
			return dao.queryPage(OrganiseEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
		else{
			throw new RuntimeException("参数不正确 !!");
		}
	}
	/** 
	 * 多条件查询结果不分页
	 */
	public List<OrganiseEntity> find(Map<String,Object> map){
		logger.debug("find {}",map);
		return (List<OrganiseEntity>)dao.queryForList(OrganiseEntity.class.getName()+".selectMulitCondition",map);
	}


    /**
     * 递归查找父节点
     * @return
     */
    public List<OrganiseEntity> getParentOrgans(String partyId, List<OrganiseEntity> parentOrgans){
//        if (IOrganiseService.DEFAULT_ORGAN_ROOT.equals(partyId)) {
//            return parentOrgans;
//        }
        OrganiseEntity parentOrg = findOrganiseEntityById(partyId);
        if(parentOrg!=null){
            parentOrgans.add(parentOrg);
            getParentOrgans(parentOrg.getParentId(), parentOrgans);
        }
        return parentOrgans;
    }

    /**
     * 获取排序号
     * @return
     */
    public int findMaxOrderNo (){

        return (int) dao.queryForObject(OrganiseEntity.class.getName()+".findMaxOrderNo");
    }

    /**
     * 判断节点是否是叶子节点
     * @param partyId
     * @return
     */
    public boolean isLeaf(String partyId){
        return 1 == (int)dao.queryForObject(OrganiseEntity.class.getName() + ".findOrganIsLeaf", partyId);
    }

	/**
	 * 获取组织链名称
	 * @param organId
	 * @return
	 */
	public List<OrganiseEntity> getOrganiseChain(String organId){
	    return (List<OrganiseEntity>) dao.queryForList(OrganiseEntity.class.getName()+".findOrganChain", organId);
	}


	/**
	 * 查找指定组织类型得父组织
	 * @param organId
	 * @param partyType
	 * @return
	 */
	public OrganiseEntity findParentOrganWithPartyType(String organId, PartyType partyType) {
		OrganiseEntity result;

		OrganiseEntity organ = dao.get(OrganiseEntity.class,organId);

		if ("root".equals(organ.getPartyId()) || (organ.getPartyTypeId() != null && organ.getPartyTypeId() == partyType.value)) {
			result = organ;
		} else {
			result = findParentOrganWithPartyType(organ.getParentId(), partyType);
		}
		return result;
	}


//    /**
//     * 不带权限的找到所有的子孙组织
//     *
//     * @param partyId
//     * @return
//     */
//    public List<OrganiseEntity> findAllOrganDes(String partyId) {
//        List<OrganiseEntity> list = new ArrayList<>();
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("parentPartyId", partyId);
//        param.put("statusId", PartyStatus.normal);
//        List<OrganiseEntity> sons = findAllSon(param);
//        if (sons != null && !sons.isEmpty()) {
//            list.addAll(sons);
//            for (Organise organise : sons) {
//                List<OrganiseEntity> temp = findAllOrganDes(organise.getPartyId());
//                if (!temp.isEmpty())
//                    list.addAll(temp);
//            }
//        }
//        return list;
//    }
//
//    public List<OrganiseEntity> findAllSon(Map<String, ? extends Object> param) {
//        return (List<OrganiseEntity>) dao.queryForList(OrganiseEntity.class.getName() + ".selectMulitOrgan", param);
//    }

	/**
	 * 不带权限的找到所有的子孙组织
	 * @param partyId
	 * @return
	 */
	public List<OrganiseEntity> findAllOrganDes(String partyId) {
		return (List<OrganiseEntity>) dao.queryForList(OrganiseEntity.class.getName() + ".findAllOrganDes",partyId);
	}

	/**
	 *  warn: 该方法只查询t_organise表
	 * @param parentId
	 * @return
	 */
	public List<OrganiseEntity> findByParentId(String parentId){
		return (List<OrganiseEntity>) dao.queryForList(OrganiseEntity.class.getName() + ".selectByParentId",parentId);
	}

}
